/**
 * Created by a1 on 16/8/30.
 */

(function($) {
    loadCSS("css/dm_lib.css")
    $.alerts = {
        maskOpacity: 0.4,

        // Private methods
        _show: function(title, message, confirm, callbackConfirm, cancel, callbackCancel) {
            //$.alerts._hide();
            //console.log("confirm = " + confirm + ",callbackConfirm = " + callbackConfirm + ",cancel = " + cancel + ",callbackCancel = " + callbackCancel)
            var body = $("body")
            var nodeMask = $("<div id='dm_popup_mask'></div>")
                nodeMask.css({
                    //height: $(document).height(),
                    //opacity: $.alerts.maskOpacity
                });
            nodeMask.click(function(){
                $.alerts._hide()
                if(!isEmpty(callbackCancel)){
                    callbackCancel()
                }
            })
            body.append(nodeMask)
            nodeMask.addClass("dm_show")

            var nodeContainer = $("<div id='dm_popup_container'></div>")
            nodeContainer.click(function(){
                $.alerts._hide()
            })
            body.append(nodeContainer)

            if(!isEmpty(title)) {
                var node = $("<div id='dm_popup_title'>"+title+"</div>")
                nodeContainer.append(node)
            }

            var nodeButtonContainer = $("<div id='dm_popup_button_container'></div>")
            nodeButtonContainer.addClass("dm_divider_top")
            if(!isEmpty(message)) {
                var node = $("<div id='dm_popup_msg'>"+message+"</div>")
                nodeContainer.append(node)
            }
            nodeContainer.append(nodeButtonContainer)

            if(isEmpty(confirm)) {
                confirm = "确定"
            }
            var nodeButtonConfirm = $("<div id='dm_popup_button'>"+confirm+"</div>")
            nodeButtonConfirm.click(function(){
                $.alerts._hide()
                if(!isEmpty(callbackConfirm)){
                    callbackConfirm()
                }
            })
            nodeButtonContainer.append(nodeButtonConfirm)
            if(!isEmpty(cancel)) {
                nodeButtonConfirm.addClass("dm_confirm")
                var nodeButtonCancel = $("<div id='dm_popup_button' class='dm_cancel'>"+cancel+"</div>")
                nodeButtonCancel.click(function(){
                    $.alerts._hide()
                    if(!isEmpty(callbackCancel)){
                        callbackCancel()
                    }
                })
                nodeButtonContainer.append(nodeButtonCancel)
            }
            $.alerts._reposition()
            disableScroll()
        },

        _hide: function() {
            $("#dm_popup_mask").remove();
            $("#dm_popup_container").remove();
            enableScroll()
        },

        _reposition: function() {
            var top = (($(window).height() / 2) - ($("#dm_popup_container").outerHeight() / 2));
            var left = (($(window).width() / 2) - ($("#dm_popup_container").outerWidth() / 2));
            //top += $(window).scrollTop()
            if( top < 0 ) top = 0;
            if( left < 0 ) left = 0;

            $("#dm_popup_container").css({
                top: top + 'px',
                left: left + 'px'
            });
            $("#dm_popup_mask").height( $(document).height() );
        },
    }
    dm_alert = function(title, message, confirm, callbackConfirm, cancel, callbackCancel) {
        $.alerts._show(title, message, confirm, callbackConfirm, cancel, callbackCancel)
    }
})(jQuery);

function loadCSS(filename) {
    var fileref = document.createElement('link');
    fileref.setAttribute("rel","stylesheet");
    fileref.setAttribute("type","text/css");
    fileref.setAttribute("href",filename);
    document.getElementsByTagName("head")[0].appendChild(fileref);
}

function isEmpty(str) {
    return str == null || "" == str || undefined == str
}

//scroll
function disableScroll() {
    document.ontouchmove = function(){
        return false
    }
}

function enableScroll() {
    document.ontouchmove = function(){
        return true
    }
}